cmake_minimum_required(VERSION 3.16)
project(platform)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FeatureSummary)
find_package(ECM 5.92.0  NO_MODULE)
set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://commits.kde.org/extra-cmake-modules")
feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake/Modules)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick Qml X11Extras Widgets DBus REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick Qml X11Extras Widgets DBus REQUIRED)
find_package(PkgConfig REQUIRED)
find_package(KF5WindowSystem REQUIRED)
find_package(KF5Wayland)
find_package(WaylandScanner)
find_package(WaylandProtocols 1.15)
set_package_properties(WaylandProtocols PROPERTIES TYPE REQUIRED)
find_package(KylinWaylandProtocols 1.0.0 CONFIG)
set_package_properties(KylinWaylandProtocols PROPERTIES TYPE REQUIRED)
find_package(PlasmaWaylandProtocols 1.4.0 CONFIG)
set_package_properties(PlasmaWaylandProtocols PROPERTIES TYPE REQUIRED)

set(EXTERNAL_LIBS "")
set(PC_PKGS kysdk-waylandhelper gsettings-qt x11 Qt5Xdg)
foreach(external_lib IN ITEMS ${PC_PKGS})
    pkg_check_modules(${external_lib} REQUIRED IMPORTED_TARGET ${external_lib})
    if(${${external_lib}_FOUND})
        include_directories(${${external_lib}_INCLUDE_DIRS})
        link_directories(${${external_lib}_LIBRARY_DIRS})
        list(APPEND EXTERNAL_LIBS PkgConfig::${external_lib})
    endif()
endforeach()

set(UKUI_QUICK_PLATFORM_SRCS
        ukui/settings.cpp
        ukui/app-launcher.cpp
        ukui/ukui-theme-proxy.cpp
        ukui/screen-area-utils.cpp
        wayland/registry.cpp
        wayland/wayland-pointer_p.h
        wayland/wayland-integration.cpp
        windows/dialog.cpp
        windows/ukui-window.cpp
        window-helper.cpp
        wm-interface.cpp
        wm-impl-x11.h wm-impl-x11.cpp
        wm-impl-wayland.cpp wm-impl-wayland.h
)
set(HEADERS
        ukui/settings.h
        ukui/app-launcher.h
        ukui/ukui-theme-proxy.h
        ukui/screen-area-utils.h
        window-type.h
        wm-interface.h
        window-helper.h
        wayland/registry.h
        windows/dialog.h
        windows/ukui-window.h
)
include_directories(wayland)
ecm_add_wayland_client_protocol(UKUI_QUICK_PLATFORM_SRCS
        PROTOCOL ${KYLIN_WAYLAND_PROTOCOLS_DIR}/ukui-shell.xml
        BASENAME ukui-shell
)

ecm_add_wayland_client_protocol(UKUI_QUICK_PLATFORM_SRCS
        PROTOCOL ${KYLIN_WAYLAND_PROTOCOLS_DIR}/ukui-window-management.xml
        BASENAME ukui-window-management
)

ecm_add_wayland_client_protocol(UKUI_QUICK_PLATFORM_SRCS
        PROTOCOL ${PLASMA_WAYLAND_PROTOCOLS_DIR}/plasma-virtual-desktop.xml
        BASENAME plasma-virtual-desktop
)

add_library(${PROJECT_NAME} SHARED
        ${UKUI_QUICK_PLATFORM_SRCS} ${HEADERS}
)
add_library(${ROOT_PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

target_link_libraries(${PROJECT_NAME}
        PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
        Qt${QT_VERSION_MAJOR}::Quick
        Qt${QT_VERSION_MAJOR}::Qml
        Qt${QT_VERSION_MAJOR}::X11Extras
        Qt${QT_VERSION_MAJOR}::Widgets
        Qt${QT_VERSION_MAJOR}::DBus
        KF5::WindowSystem
        ${EXTERNAL_LIBS}
)

set(HEADERS_INSTALL_DIR /usr/include/ukui-quick/platform)
target_include_directories(${PROJECT_NAME} INTERFACE $<INSTALL_INTERFACE:${HEADERS_INSTALL_DIR}>)

include(CMakePackageConfigHelpers)
set(CMAKE_CONFIG_INSTALL_DIR "/usr/share/cmake/ukui-quick-platform")
set(PC_INSTALL_DIR "/usr/lib/pkgconfig")

configure_package_config_file(
        "${CMAKE_CURRENT_SOURCE_DIR}/ukui-quick-platform-config.cmake.in"
        "${CMAKE_CURRENT_BINARY_DIR}/ukui-quick-platform-config.cmake"
        INSTALL_DESTINATION  ${CMAKE_CONFIG_INSTALL_DIR}
)

write_basic_package_version_file(
        ${CMAKE_CURRENT_BINARY_DIR}/ukui-quick-platform-config-version.cmake
        VERSION ${UKUI_QUICK_VERSION}
        COMPATIBILITY SameMajorVersion
)

configure_package_config_file(
        "${CMAKE_CURRENT_SOURCE_DIR}/ukui-quick-platform.pc.in"
        "${CMAKE_CURRENT_BINARY_DIR}/ukui-quick-platform.pc"
        INSTALL_DESTINATION  ${PC_INSTALL_DIR})

set_target_properties(${PROJECT_NAME} PROPERTIES
        VERSION ${UKUI_QUICK_VERSION}
        SOVERSION ${VERSION_MAJOR}
        OUTPUT_NAME ${ROOT_PROJECT_NAME}-${PROJECT_NAME}
)
install(TARGETS ${PROJECT_NAME}
        EXPORT ${PROJECT_NAME}
        PUBLIC_HEADER DESTINATION ${HEADERS_INSTALL_DIR}
        LIBRARY DESTINATION /usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}
)
install(EXPORT ${PROJECT_NAME}
        FILE ukui-quick-platform-targets.cmake
        NAMESPACE ${ROOT_PROJECT_NAME}::
        DESTINATION ${CMAKE_CONFIG_INSTALL_DIR})

install(FILES ${HEADERS} DESTINATION ${HEADERS_INSTALL_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ukui-quick-platform.pc DESTINATION ${PC_INSTALL_DIR})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/ukui-quick-platform-config.cmake
        ${CMAKE_CURRENT_BINARY_DIR}/ukui-quick-platform-config-version.cmake
        DESTINATION ${CMAKE_CONFIG_INSTALL_DIR})

set(UKUI_QUICK_PLATFORM_PLUGIN_SRCS
        ukui-quick-platform-plugin.cpp
        ukui-quick-platform-plugin.h
)
add_library(${ROOT_PROJECT_NAME}-${PROJECT_NAME}-plugin SHARED ${UKUI_QUICK_PLATFORM_PLUGIN_SRCS})

target_compile_definitions(${ROOT_PROJECT_NAME}-${PROJECT_NAME}-plugin PRIVATE
        PLUGIN_IMPORT_URI="org.ukui.quick.platform"
        PLUGIN_VERSION_MAJOR=${VERSION_MAJOR}
        PLUGIN_VERSION_MINOR=${VERSION_MINOR}
)
target_link_libraries(${ROOT_PROJECT_NAME}-${PROJECT_NAME}-plugin
        PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
        Qt${QT_VERSION_MAJOR}::Qml
        Qt${QT_VERSION_MAJOR}::Quick
        ${PROJECT_NAME}
)
set(PLUGIN_INSTALL_ROOT_PATH "/usr/lib/${CMAKE_LIBRARY_ARCHITECTURE}/qt5/qml/org/ukui/quick/platform")
install(FILES qmldir DESTINATION "${PLUGIN_INSTALL_ROOT_PATH}")
install(TARGETS ${ROOT_PROJECT_NAME}-${PROJECT_NAME}-plugin LIBRARY DESTINATION "${PLUGIN_INSTALL_ROOT_PATH}")

if(BUILD_TEST)
    add_subdirectory(test)
endif ()
